package com.pda.xinma.presenter;

import com.pda.xinma.bean.DeliveryBasicInfoBean;
import com.pda.xinma.bean.HnadingInDetailListBean;
import com.pda.xinma.bean.WaitOutStockListBean;
import com.pda.xinma.bean.request.ConfirmStockRequest;
import com.pda.xinma.contract.HandingOutContract;
import com.pda.xinma.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;

public class HandingOutPresenter extends BasePresenter<HandingOutContract.View> implements HandingOutContract.Presenter {


    @Override
    public void queryDeliveryBasicInfo(String entryNo) {
        NetUtils.geApi().queryDeliveryBasicInfo(entryNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<DeliveryBasicInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<DeliveryBasicInfoBean> deliveryBasicInfoBeanBaseAppEntity) throws Exception {
                        getView().setDetail(deliveryBasicInfoBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void queryMatchingStock(String deliveryNo, String numId) {
        NetUtils.geApi().queryMatchingStock(deliveryNo, numId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<WaitOutStockListBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<WaitOutStockListBean>> listBaseAppEntity) throws Exception {
                        getView().setMatchingStockList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void queryAutoMatchingStock(String deliveryNo, String numId) {
        NetUtils.geApi().queryAutoMatchingStock(deliveryNo, numId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<HnadingInDetailListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<HnadingInDetailListBean> hnadingInDetailListBeanBaseAppEntity) throws Exception {
                        getView().onAutoMatchingSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void confirmStock(ConfirmStockRequest request) {
        NetUtils.geApi().confirmStock(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<HnadingInDetailListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<HnadingInDetailListBean> hnadingInDetailListBeanBaseAppEntity) throws Exception {
                        getView().onAutoMatchingSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void queryWaitOutStockList(String deliveryNo) {
        NetUtils.geApi().queryWaitOutStockList(deliveryNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<WaitOutStockListBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<WaitOutStockListBean>> listBaseAppEntity) throws Exception {
                        getView().setWaitOutStockList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void pailOut(String deliveryNo, String numId) {
        NetUtils.geApi().pailOut(deliveryNo, numId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<HnadingInDetailListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<HnadingInDetailListBean> hnadingInDetailListBeanBaseAppEntity) throws Exception {
                        getView().showMessage(hnadingInDetailListBeanBaseAppEntity.getMessage());
                        getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
